#include <stdafx.h>
#include <XWindow.h>

XWindow::XWindow(XHash hash)
{
	Hash = hash;
	Rectangle.X = 50;
	Rectangle.Y = 50;
	Rectangle.W = 300;
	Rectangle.H = 200;

	WindowLayout = NULL;
	WindowTitle = NULL;
	Texture = NULL;
	Moveable = false;
}

XWindow::~XWindow()
{
	delete Texture;
	delete WindowLayout;
	delete WindowTitle;
}


void XWindow::SetGeometry(XRectangle rect)
{
	Rectangle = rect;
	_loadVertices();

	if(WindowTitle != NULL)
		WindowTitle->SetTextBox(XRectangle(Rectangle.X + 4, Rectangle.Y + 4, Rectangle.W - 8, 26 - 8));
}

XRectangle* XWindow::GetGeometry()
{
	return &Rectangle;
}

void XWindow::SetMoveable(bool moveable)
{
	Moveable = moveable;
}

bool XWindow::GetMoveable()
{
	return Moveable;
}

XFont* XWindow::GetFont()
{
	return Font;
}

XTexture* XWindow::GetTexture()
{
	return Texture;
}

void XWindow::RegisterControlsRender(XRenderPack* render)
{
	ControlsRender.push_back(render);
}

void XWindow::RegisterControlsText(XTextPack* text)
{
	ControlsText.push_back(text);
}

void XWindow::_loadVertices()
{
	if(WindowLayout != NULL)
	{
		XVertex* vert = new XVertex[6];
		vert[0].X = Rectangle.X;
		vert[0].Y = Rectangle.Y;
		vert[1].X = Rectangle.X + Rectangle.W;
		vert[1].Y = Rectangle.Y;
		vert[2].X = Rectangle.X;
		vert[2].Y = Rectangle.Y + 30;
		vert[3].X = Rectangle.X + Rectangle.W;
		vert[3].Y = Rectangle.Y + 30;
		vert[4].X = Rectangle.X;
		vert[4].Y = Rectangle.Y + Rectangle.H;
		vert[5].X = Rectangle.X + Rectangle.W;
		vert[5].Y = Rectangle.Y + Rectangle.H;

		WindowLayout->SetVertex(vert, 6);
		delete[] vert;
	}
}

void XWindow::_loadTextureVertices()
{
	if(Texture != NULL)
	{
		XVertex* vert = new XVertex[6];
		vert[0].X = (float)4/Texture->GetWidth();
		vert[0].Y = (float)4/Texture->GetHeight();
		vert[1].X = (float)505/Texture->GetWidth();
		vert[1].Y = (float)4/Texture->GetHeight();
		vert[2].X = (float)4/Texture->GetWidth();
		vert[2].Y = (float)30/Texture->GetHeight();
		vert[3].X = (float)505/Texture->GetWidth();
		vert[3].Y = (float)30/Texture->GetHeight();
		vert[4].X = (float)4/Texture->GetWidth();
		vert[4].Y = (float)330/Texture->GetHeight();
		vert[5].X = (float)505/Texture->GetWidth();
		vert[5].Y = (float)330/Texture->GetHeight();

		WindowLayout->SetTexture(Texture, vert, 6);
		delete[] vert;
	}
}

void XWindow::_register(XTexture* texture, XFont* font)
{
	WindowTitle = new XTextPack(Hash.String + "-Title");
	WindowTitle->SetTextBox(XRectangle(Rectangle.X + 4, Rectangle.Y + 4, Rectangle.W - 8, 26 - 8));
	WindowTitle->SetColor(XColor(160, 160, 160, 255));
	WindowTitle->SetText(Hash.String);
	WindowTitle->SetFont(font);

	WindowLayout = new XRenderPack(Hash.String + "-Layout");
	Font = font;
	Texture = texture;

	_loadTextureVertices();
	_loadVertices();

}

void XWindow::Register()
{
	
}

void XWindow::Unregister()
{

}

void XWindow::_moveableUpdate(XInputModule* input)
{
	if(Moveable == true)
	{
		XPosition pos = input->GetMouse().GetPosition();
		if(MousePosition.X != -1 && MousePosition.Y != -1 && input->GetMouse().GetLeft().GetCurrent() == true)
		{
			SetGeometry(XRectangle(Rectangle.X + pos.X - MousePosition.X, Rectangle.Y + pos.Y - MousePosition.Y, Rectangle.W, Rectangle.H));
			MousePosition = pos;
		}
		else if(pos.X > Rectangle.X && pos.X < Rectangle.X + Rectangle.W && pos.Y > Rectangle.Y && pos.Y < Rectangle.Y + 30)
		{
			if(input->GetMouse().GetLeft().GetCurrent() == true && input->GetMouse().GetLeft().GetPrevious() == false)
			{
				MousePosition = pos;
			}
			else
			{
				MousePosition.X = -1;
				MousePosition.Y = -1;
			}
		}
		else
		{
			MousePosition.X = -1;
			MousePosition.Y = -1;
		}

	}
}

void XWindow::Update(XInputModule* input)
{
	if(Active)
		_moveableUpdate(input);

}